package com.lvhx.springboot.base;

import com.lvhx.springboot.entity.User;
import com.lvhx.springboot.mapper.extend.UserExtendMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 聚合的实体类
 * 里面将聚合包含bean的字段全部写入
 * 实现了更新和删除聚合的方法
 */
@Slf4j
public class AccountUser extends AbstractAggregateRoot {
    private Long id;

    private String name;

    private String password;

    private String salt;

    private String email;

    private String mobile;

    private Byte status;

    private Long deptId;

    private String createBy;

    private Date createTime;

    private String lastUpdateBy;

    private Date lastUpdateTime;

    private Byte delFlag;

    private boolean lockYn;
    private UserExtendMapper userExtendMapper;

    @Override
    protected int doDelete() {
        return 0;
    }

    @Override
    protected int doUpdate() {
        if (this.lockYn) {
            log.info("=====此次更新用户聚合id:{}", this.getId());
            log.info("=====此次更新用户聚合Map:{}", this.getChangedCache());
            userExtendMapper.updateAccountAggregate(this.id, this.getVersion(), this.getChangedCache());
            return 1;
        } else {
            return 1;
        }
    }

    public AccountUser() {
    }

    public AccountUser(User user, boolean lockYn, UserExtendMapper userExtendMapper) {
        id = user.getId();
        name = user.getName();
        password = user.getPassword();
        this.lockYn = lockYn;
        this.userExtendMapper = userExtendMapper;
        setVersion(user.getVersion());
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("name", name);
        }
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("password", password);
        }
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("salt", salt);
        }
        this.salt = salt;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("email", email);
        }
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("mobile", mobile);
        }
        this.mobile = mobile;
    }

    public Byte getStatus() {
        return status;
    }

    public void setStatus(Byte status) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("status", status);
        }
        this.status = status;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("deptId", deptId);
        }
        this.deptId = deptId;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("createBy", createBy);
        }
        this.createBy = createBy;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getLastUpdateBy() {
        return lastUpdateBy;
    }

    public void setLastUpdateBy(String lastUpdateBy) {
        this.lastUpdateBy = lastUpdateBy;
    }

    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public Byte getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(Byte delFlag) {
        if (!isInitializing()) {
            this.markDirty();
            this.getChangedCache().put("delFlag", delFlag);
        }
        this.delFlag = delFlag;
    }
}
